class ExampleController < ApplicationController
  verify :params =>:id, :render => {:text=>"Input Error:: Please input dictionary ids or keywords."},:only => :search
  before_filter :prepare_properties, :only => :search

  def index
    redirect_to :controller => :dictionary
  end
  
  def search # 例文の検索を行う
    # パラメータから検索したい見出し語のIDを受け取る
    if /^[\d,]+$/ =~ params[:id]
      @ids = params[:id].split(",").map{|id| id.to_i }
    else
      @ids = params[:id].split(",")
    end
    @reibun = Hash.new{|hash,key| hash[key] = Hash.new{|h,k| h[k] = Array.new }}
    # 例文を検索
    ActiveRecord::Base.transaction do
			Assign.make_for @ids
      Example.make_for @ids,@min_length,@max_length,@vocabulary,@kanji,@corpus,@complexity,@vocabulary_weight,@kanji_weight,@corpus_weight,@complexity_weight,@length_weight
      #@reibun["total"] = Example.count_by_sql "SELECT COUNT(*) FROM temporary_examples;"
      # 見出し語→語義
      Dictionary.find(:all,:from => :temporary_examples,:group => "dictionary_id,sense_position",:select => "dictionary_id,sense_position").each do |line|
        if line.sense_position.blank?
          condition = ["dictionary_id = ? AND sense_position IS NULL",line.dictionary_id.to_i]
        else
          condition = ["dictionary_id = ? AND sense_position = ?",line.dictionary_id.to_i,line.sense_position.to_i]
        end
        
        Example.find(:all,:from => :temporary_examples,:conditions => condition,:limit => 5).each do |example|
          unless example.sense_position.nil?
            @reibun[line.dictionary_id.to_i][example.sense_position.to_i] << {"data" => example.data ,"position" => example.text_position.to_i }
          else
            @reibun[line.dictionary_id.to_i]["unclassify"] << {"data" => example.data ,"position" => example.text_position.to_i }
          end
        end
      end
			#
      Example.drop_temporary
			Assign.drop_temporary
    end
    
    # YAML形式として出力
    # render :text => @reibun.to_yaml
    @sense_size = params["sense_size"].split(",").map{|id| id.to_i }
    @keyword = [[1,[[6843, "hello"]]],[1,[[6843, "hello"]]],[1,[[6843, "hello"]]]]
  end
  
  def edit # 要セキュリティ対策
    # TODO 例文に付与された語義情報を編集する
  end
  
  #private
  def prepare_properties
    #
    @max_length = params[:length][:max].blank? ? 30 : params[:length][:max].to_i 
    @min_length = params[:length][:min].blank? ? 5 : params[:length][:min].to_i
    @min_length = 0 if @max_length < @min_length
    #
    @vocabulary = params[:grade][:vocabulary].blank? ? 0.0 : (params[:grade][:vocabulary].to_f + 1.0) / 5.0
    @kanji = params[:grade][:kanji].blank? ? 0.0 : (params[:grade][:kanji].to_f + 1.0) / 5.0
    @corpus = params[:grade][:corpus].blank? ? 0.0 : (params[:grade][:corpus].to_f + 1.0) / 5.0
    @complexity = params[:grade][:complexity].blank? ? 0.0 : (params[:grade][:complexity].to_f + 1.0) / 5.0
    #
    @length_weight = params[:weight][:length].blank? ? 0.5 : params[:weight][:length].to_f
    @complexity_weight = params[:weight][:complexity].blank? ? 0.5 : params[:weight][:complexity].to_f
    @vocabulary_weight = params[:weight][:vocabulary].blank? ? 0.5 : params[:weight][:vocabulary].to_f
    @kanji_weight = params[:weight][:kanji].blank? ? 0.5 : params[:weight][:kanji].to_f
    @corpus_weight = params[:weight][:corpus].blank? ? 0.5 : params[:weight][:corpus].to_f
    true
  end
  
end
